﻿using System.Collections.Generic;
using System.Text;
using IniHelper;
using System.Windows.Forms;

namespace Client.Managers
{
    class HotKeyManager
    {
        string _hotkeyTablePath = @"config\hotkeys.ini";
        string _userPath = "";
        public Dictionary<string, Dictionary<string, string>> hotKeys { get; protected set; }

        public HotKeyManager()
        {
            hotKeys = new Dictionary<string, Dictionary<string, string>>();
            InitHotKeys();
        }

        void InitHotKeys()
        {
            IniHelper.IniHelper defaultIni = new IniHelper.IniHelper(_hotkeyTablePath);

            List<IniStruct> vs = defaultIni.ReadValues();
            foreach(IniStruct v in vs)
            {
                Dictionary<string, string> d = new Dictionary<string, string>();
                foreach(KeyValuePair<string,string> item in v)
                {
                     d[item.Value] = item.Key;
                }
                hotKeys[v.Section] = d;
            }
        }

        public string GetFuncName(string section,System.Windows.Forms.KeyEventArgs e,string[] parentsName)
        {
            string k = HotKeyInfo.GetStringByKey(e);
            if (hotKeys.ContainsKey(section) && hotKeys[section].ContainsKey(k))
                return hotKeys[section][k];
            else
            {
                foreach(var parentName in parentsName)
                {
                    if (hotKeys.ContainsKey(parentName) && hotKeys[parentName].ContainsKey(k))
                        return hotKeys[parentName][k];
                }
            }
            return "";
        }

        /*
         * 从传入的section中查找是否存在 e 代表的快捷键
         * 如果存在，返回配置中对应的key
         */ 
        public string GetFuncName(string[] sections,System.Windows.Forms.KeyEventArgs e)
        {
            string k = HotKeyInfo.GetStringByKey(e);

            foreach(string section in sections)
            {
                if (hotKeys[section].ContainsKey(k))
                    return hotKeys[section][k];
            }
            return "";
        }
    }

    public static class HotKeyInfo
    {
        // 根据KeyEventArgs生成组合键字符串
        public static string GetStringByKey(KeyEventArgs e)
        {
            if (e.KeyValue == 16)
            {
                return "Shift + ";
            }
            else if (e.KeyValue == 17)
            {
                return "Ctrl + ";
            }
            else if (e.KeyValue == 18)
            {
                return "Alt + ";
            }
            else
            {
                StringBuilder keyValue = new StringBuilder();
                if (e.Modifiers != 0)
                {
                    if (e.Control)
                    {
                        keyValue.Append("Ctrl + ");
                    }
                    if (e.Alt)
                    {
                        keyValue.Append("Alt + ");
                    }
                    if (e.Shift)
                    {
                        keyValue.Append("Shift + ");
                    }
                }
                if ((e.KeyValue >= 48 && e.KeyValue <= 57))    //0-9
                {
                    keyValue.Append(e.KeyCode.ToString());
                    //keyValue.Append(e.KeyCode.ToString().Substring(1));
                }
                else if (e.KeyValue == 221)
                {
                    keyValue.Append("OemCloseBrackets");
                }
                else
                {
                    keyValue.Append(e.KeyCode);
                }

                return keyValue.ToString();
            }
        }

        //  根据按键获得单一键值对应字符串
        public static string GetSingleStrByKey(KeyEventArgs e)
        {
            if (e.KeyValue == 16)
            {
                return "Shift";
            }
            else if (e.KeyValue == 17)
            {
                return "Ctrl";
            }
            else if (e.KeyValue == 18)
            {
                return "Alt";
            }
            else
            {
                return e.KeyCode.ToString();
            }
        }

        // 根据string生成KeyEventArgs
        public static KeyEventArgs GetKeyByString(string strKey)
        {
            Keys keyResult = new Keys();
            string[] strKeyCodes = strKey.Split('+');
            if (strKeyCodes.Length > 0)
            {
                int numberKey;
                foreach (string keyEach in strKeyCodes)
                {
                    if (keyEach.Trim().ToUpper() == "CTRL")
                    {
                        keyResult = keyResult | Keys.Control;
                    }
                    else if (keyEach.Trim().ToUpper() == "SHIFT")
                    {
                        keyResult = keyResult | Keys.Shift;
                    }
                    else if (keyEach.Trim().ToUpper() == "ALT")
                    {
                        keyResult = keyResult | Keys.Alt;
                    }
                    //数字
                    else if (int.TryParse(keyEach, out numberKey))
                    {
                        KeysConverter converter = new KeysConverter();
                        Keys getKey = (Keys)converter.ConvertFromString('D' + keyEach);
                        keyResult = keyResult | getKey;
                    }
                    //其他（字母，F0-F12)
                    else
                    {
                        KeysConverter converter = new KeysConverter();
                        Keys getKey = (Keys)converter.ConvertFromString(keyEach);
                        keyResult = keyResult | getKey;
                    }
                }

            }
            KeyEventArgs newEventArgs = new KeyEventArgs(keyResult);
            return newEventArgs;
        }
    }
}
